package com.kuang.bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @program: juc
 * @description: 阻塞队列 FIFO
 * @author: 陈恩涛
 * @create: 2021-03-05 11:13
 **/
public class Test {
    public static void main(String[] args) throws InterruptedException {
        // Collection
        // List
        // Set
        // BlockingQueue

        test1();
        test2();
        test3();
        test4();
    }

    /*
     * 抛出异常
     * */
    public static void test1() {

        // 队列大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));

        // java.lang.IllegalStateException: Queue full
        // System.out.println(blockingQueue.add("d"));

        System.out.println(blockingQueue.element()); //查看队首元素

        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());

        // java.util.NoSuchElementException
        // System.out.println(blockingQueue.remove());

    }

    /*
     * 有返回值，不抛出异常
     * */
    public static void test2() {
        // 队列大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d")); //不抛出异常，返回false

        System.out.println(blockingQueue.peek()); //查看队首元素

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll()); //不抛出异常，返回null

    }


    /**
     *方法描述： 等待，阻塞（一直阻塞）
     *
     * @param
     * @return void
     * @author chenentao
     * @date 2021/3/5 17:30
     */
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println("================");
        // 一直阻塞
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        // blockingQueue.put("d");  //队列没有位置了，一直阻塞
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        // System.out.println(blockingQueue.take()); //没有这个元素，一直阻塞
    }

    /**
     *方法描述： 等待，阻塞（等待超时）
     *
     * @param
     * @return void
     * @author chenentao
     * @date 2021/3/5 17:30
     */
    public static void test4() throws InterruptedException {
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println("=========================");
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d", 2, TimeUnit.SECONDS)); //阻塞，等待，超过两秒就返回false

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS)); //阻塞，等待，超过两秒就返回null
    }
}
